package cn.qqtheme.framework.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.internal.view.SupportMenu;
import android.support.v4.view.InputDeviceCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/* loaded from: classes.dex */
public class ColorPanelView extends View {
    private ColorPanelView mBrightnessGradientView;
    private RectF mGradientRect;
    private float[] mHSV;
    private boolean mIsBrightnessGradient;
    private int mLastX;
    private int mLastY;
    private boolean mLockPointerInBounds;
    private OnColorChangedListener mOnColorChangedListener;
    private Paint mPaint;
    private Paint mPaintBackground;
    private Drawable mPointerDrawable;
    private int mPointerHeight;
    private int mPointerWidth;
    private float mRadius;
    private int mSelectedColor;
    private int[] mSelectedColorGradient;
    private Shader mShader;
    private static final int[] GRAD_COLORS = {SupportMenu.CATEGORY_MASK, InputDeviceCompat.SOURCE_ANY, -16711936, -16711681, -16776961, -65281, SupportMenu.CATEGORY_MASK};
    private static final int[] GRAD_ALPHA = {-1, 0};

    /* loaded from: classes.dex */
    public interface OnColorChangedListener {
        void onColorChanged(ColorPanelView colorPanelView, int i);
    }

    public ColorPanelView(Context context) {
        super(context);
        this.mGradientRect = new RectF();
        this.mHSV = new float[]{1.0f, 1.0f, 1.0f};
        this.mSelectedColorGradient = new int[]{0, -16777216};
        this.mRadius = 0.0f;
        this.mSelectedColor = 0;
        this.mIsBrightnessGradient = false;
        this.mLastX = Integer.MIN_VALUE;
        this.mLockPointerInBounds = false;
        init();
    }

    public ColorPanelView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mGradientRect = new RectF();
        this.mHSV = new float[]{1.0f, 1.0f, 1.0f};
        this.mSelectedColorGradient = new int[]{0, -16777216};
        this.mRadius = 0.0f;
        this.mSelectedColor = 0;
        this.mIsBrightnessGradient = false;
        this.mLastX = Integer.MIN_VALUE;
        this.mLockPointerInBounds = false;
        init();
    }

    public ColorPanelView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.mGradientRect = new RectF();
        this.mHSV = new float[]{1.0f, 1.0f, 1.0f};
        this.mSelectedColorGradient = new int[]{0, -16777216};
        this.mRadius = 0.0f;
        this.mSelectedColor = 0;
        this.mIsBrightnessGradient = false;
        this.mLastX = Integer.MIN_VALUE;
        this.mLockPointerInBounds = false;
        init();
    }

    private void init() {
        setClickable(true);
        this.mPaint = new Paint(1);
        this.mPaintBackground = new Paint(1);
        this.mPaintBackground.setColor(-1);
        setLayerType(1, isInEditMode() ? null : this.mPaint);
    }

    @Override // android.view.View
    protected void onMeasure(int i, int i2) {
        int i3;
        Drawable drawable = this.mPointerDrawable;
        int i4 = 0;
        if (drawable != null) {
            i4 = drawable.getIntrinsicHeight();
            i3 = this.mPointerDrawable.getIntrinsicWidth();
        } else {
            i3 = 0;
        }
        int mode = View.MeasureSpec.getMode(i);
        int size = View.MeasureSpec.getSize(i);
        int mode2 = View.MeasureSpec.getMode(i2);
        int size2 = View.MeasureSpec.getSize(i2);
        if (mode == 1073741824) {
            i3 = size;
        } else if (mode == Integer.MIN_VALUE) {
            i3 = Math.min(i3, size);
        }
        if (mode2 == 1073741824) {
            i4 = size2;
        } else if (mode2 == Integer.MIN_VALUE) {
            i4 = Math.min(i4, size2);
        }
        setMeasuredDimension(i3, i4);
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.mShader != null) {
            RectF rectF = this.mGradientRect;
            float f = this.mRadius;
            canvas.drawRoundRect(rectF, f, f, this.mPaintBackground);
            RectF rectF2 = this.mGradientRect;
            float f2 = this.mRadius;
            canvas.drawRoundRect(rectF2, f2, f2, this.mPaint);
        }
        onDrawPointer(canvas);
    }

    private void onDrawPointer(Canvas canvas) {
        float max;
        float f;
        if (this.mPointerDrawable != null) {
            int height = getHeight();
            int i = this.mPointerWidth >> 1;
            int i2 = this.mPointerHeight;
            int i3 = i2 >> 1;
            if (!this.mIsBrightnessGradient) {
                float f2 = this.mLastX - i;
                float f3 = this.mLastY - i3;
                if (this.mLockPointerInBounds) {
                    f = Math.max(this.mGradientRect.left, Math.min(f2, this.mGradientRect.right - this.mPointerWidth));
                    max = Math.max(this.mGradientRect.top, Math.min(f3, this.mGradientRect.bottom - this.mPointerHeight));
                } else {
                    float f4 = i;
                    f = Math.max(this.mGradientRect.left - f4, Math.min(f2, this.mGradientRect.right - f4));
                    max = Math.max(this.mGradientRect.top - f4, Math.min(f3, this.mGradientRect.bottom - i3));
                }
            } else {
                float f5 = this.mLastX - i;
                float f6 = i2 != this.mPointerDrawable.getIntrinsicHeight() ? (height >> 1) - i3 : 0.0f;
                if (this.mLockPointerInBounds) {
                    float max2 = Math.max(this.mGradientRect.left, Math.min(f5, this.mGradientRect.right - this.mPointerWidth));
                    max = Math.max(this.mGradientRect.top, Math.min(f6, this.mGradientRect.bottom - this.mPointerHeight));
                    f = max2;
                } else {
                    float f7 = i;
                    float max3 = Math.max(this.mGradientRect.left - f7, Math.min(f5, this.mGradientRect.right - f7));
                    max = Math.max(this.mGradientRect.top - f7, Math.min(f6, this.mGradientRect.bottom - i3));
                    f = max3;
                }
            }
            canvas.translate(f, max);
            this.mPointerDrawable.draw(canvas);
            canvas.translate(-f, -max);
        }
    }

    @Override // android.view.View
    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        super.onLayout(z, i, i2, i3, i4);
        this.mGradientRect.set(getPaddingLeft(), getPaddingTop(), (i3 - i) - getPaddingRight(), (i4 - i2) - getPaddingBottom());
        if (z) {
            buildShader();
        }
        if (this.mPointerDrawable != null) {
            int height = (int) this.mGradientRect.height();
            int intrinsicHeight = this.mPointerDrawable.getIntrinsicHeight();
            int intrinsicWidth = this.mPointerDrawable.getIntrinsicWidth();
            this.mPointerHeight = intrinsicHeight;
            this.mPointerWidth = intrinsicWidth;
            if (height < intrinsicHeight) {
                this.mPointerHeight = height;
                this.mPointerWidth = (int) (intrinsicWidth * (height / intrinsicHeight));
            }
            this.mPointerDrawable.setBounds(0, 0, this.mPointerWidth, this.mPointerHeight);
            updatePointerPosition();
        }
    }

    private void buildShader() {
        if (this.mIsBrightnessGradient) {
            this.mShader = new LinearGradient(this.mGradientRect.left, this.mGradientRect.top, this.mGradientRect.right, this.mGradientRect.top, this.mSelectedColorGradient, (float[]) null, Shader.TileMode.CLAMP);
        } else {
            this.mShader = new ComposeShader(new LinearGradient(0.0f, (this.mGradientRect.height() / 3.0f) + this.mGradientRect.top, 0.0f, this.mGradientRect.bottom, GRAD_ALPHA, (float[]) null, Shader.TileMode.CLAMP), new LinearGradient(this.mGradientRect.left, this.mGradientRect.top, this.mGradientRect.right, this.mGradientRect.top, GRAD_COLORS, (float[]) null, Shader.TileMode.CLAMP), PorterDuff.Mode.MULTIPLY);
        }
        this.mPaint.setShader(this.mShader);
    }

    public void setRadius(float f) {
        if (((int) f) != ((int) this.mRadius)) {
            this.mRadius = f;
            invalidate();
        }
    }

    @Override // android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        this.mLastX = (int) motionEvent.getX();
        this.mLastY = (int) motionEvent.getY();
        onUpdateColorSelection(this.mLastX, this.mLastY);
        invalidate();
        int action = motionEvent.getAction();
        if (action == 0) {
            getParent().requestDisallowInterceptTouchEvent(true);
        } else if (action == 1 || action == 3) {
            getParent().requestDisallowInterceptTouchEvent(false);
        }
        return super.onTouchEvent(motionEvent);
    }

    protected void onUpdateColorSelection(int i, int i2) {
        int max = (int) Math.max(this.mGradientRect.left, Math.min(i, this.mGradientRect.right));
        int max2 = (int) Math.max(this.mGradientRect.top, Math.min(i2, this.mGradientRect.bottom));
        if (this.mIsBrightnessGradient) {
            float pointToValueBrightness = pointToValueBrightness(max);
            float[] fArr = this.mHSV;
            fArr[2] = pointToValueBrightness;
            this.mSelectedColor = Color.HSVToColor(fArr);
        } else {
            float pointToHue = pointToHue(max);
            float pointToSaturation = pointToSaturation(max2);
            float[] fArr2 = this.mHSV;
            fArr2[0] = pointToHue;
            fArr2[1] = pointToSaturation;
            fArr2[2] = 1.0f;
            this.mSelectedColor = Color.HSVToColor(fArr2);
        }
        dispatchColorChanged(this.mSelectedColor);
    }

    protected void dispatchColorChanged(int i) {
        ColorPanelView colorPanelView = this.mBrightnessGradientView;
        if (colorPanelView != null) {
            colorPanelView.setColor(i, false);
        }
        OnColorChangedListener onColorChangedListener = this.mOnColorChangedListener;
        if (onColorChangedListener != null) {
            onColorChangedListener.onColorChanged(this, i);
        }
    }

    public void setIsBrightnessGradient(boolean z) {
        this.mIsBrightnessGradient = z;
    }

    public void setBrightnessGradientView(ColorPanelView colorPanelView) {
        if (this.mBrightnessGradientView != colorPanelView) {
            this.mBrightnessGradientView = colorPanelView;
            ColorPanelView colorPanelView2 = this.mBrightnessGradientView;
            if (colorPanelView2 != null) {
                colorPanelView2.setIsBrightnessGradient(true);
                this.mBrightnessGradientView.setColor(this.mSelectedColor);
            }
        }
    }

    public int getSelectedColor() {
        return this.mSelectedColor;
    }

    public void setColor(int i) {
        setColor(i, true);
    }

    protected void setColor(int i, boolean z) {
        Color.colorToHSV(i, this.mHSV);
        if (this.mIsBrightnessGradient) {
            this.mSelectedColorGradient[0] = getColorForGradient(this.mHSV);
            this.mSelectedColor = Color.HSVToColor(this.mHSV);
            buildShader();
            int i2 = this.mLastX;
            if (i2 != Integer.MIN_VALUE) {
                this.mHSV[2] = pointToValueBrightness(i2);
            }
            i = Color.HSVToColor(this.mHSV);
        }
        if (z) {
            updatePointerPosition();
        }
        this.mSelectedColor = i;
        invalidate();
        dispatchColorChanged(this.mSelectedColor);
    }

    private int getColorForGradient(float[] fArr) {
        if (((int) fArr[2]) != 1) {
            float f = fArr[2];
            fArr[2] = 1.0f;
            int HSVToColor = Color.HSVToColor(fArr);
            fArr[2] = f;
            return HSVToColor;
        }
        return Color.HSVToColor(fArr);
    }

    private void updatePointerPosition() {
        if (this.mGradientRect.width() == 0.0f || this.mGradientRect.height() == 0.0f) {
            return;
        }
        if (!this.mIsBrightnessGradient) {
            this.mLastX = hueToPoint(this.mHSV[0]);
            this.mLastY = saturationToPoint(this.mHSV[1]);
            return;
        }
        this.mLastX = brightnessToPoint(this.mHSV[2]);
    }

    public void setOnColorChangedListener(OnColorChangedListener onColorChangedListener) {
        this.mOnColorChangedListener = onColorChangedListener;
    }

    private float pointToHue(float f) {
        return ((f - this.mGradientRect.left) * 360.0f) / this.mGradientRect.width();
    }

    private int hueToPoint(float f) {
        return (int) (this.mGradientRect.left + ((f * this.mGradientRect.width()) / 360.0f));
    }

    private float pointToSaturation(float f) {
        return 1.0f - ((1.0f / this.mGradientRect.height()) * (f - this.mGradientRect.top));
    }

    private int saturationToPoint(float f) {
        return (int) (this.mGradientRect.top + (this.mGradientRect.height() * (1.0f - f)));
    }

    private float pointToValueBrightness(float f) {
        return 1.0f - ((1.0f / this.mGradientRect.width()) * (f - this.mGradientRect.left));
    }

    private int brightnessToPoint(float f) {
        return (int) (this.mGradientRect.left + (this.mGradientRect.width() * (1.0f - f)));
    }

    public void setPointerDrawable(Drawable drawable) {
        if (this.mPointerDrawable != drawable) {
            this.mPointerDrawable = drawable;
            requestLayout();
        }
    }

    public void recycle() {
        Bitmap bitmap;
        this.mPaint = null;
        this.mPaintBackground = null;
        Drawable drawable = this.mPointerDrawable;
        if (!(drawable instanceof BitmapDrawable) || (bitmap = ((BitmapDrawable) drawable).getBitmap()) == null || bitmap.isRecycled()) {
            return;
        }
        bitmap.recycle();
    }

    public void setLockPointerInBounds(boolean z) {
        if (z != this.mLockPointerInBounds) {
            this.mLockPointerInBounds = z;
            invalidate();
        }
    }

    @Override // android.view.View
    protected Parcelable onSaveInstanceState() {
        SavedState savedState = new SavedState(super.onSaveInstanceState());
        savedState.isBrightnessGradient = this.mIsBrightnessGradient;
        savedState.color = this.mSelectedColor;
        return savedState;
    }

    @Override // android.view.View
    protected void onRestoreInstanceState(Parcelable parcelable) {
        if (!(parcelable instanceof SavedState)) {
            super.onRestoreInstanceState(parcelable);
            return;
        }
        SavedState savedState = (SavedState) parcelable;
        super.onRestoreInstanceState(savedState.getSuperState());
        this.mIsBrightnessGradient = savedState.isBrightnessGradient;
        setColor(savedState.color, true);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class SavedState extends View.BaseSavedState {
        public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() { // from class: cn.qqtheme.framework.widget.ColorPanelView.SavedState.1
            /* JADX WARN: Can't rename method to resolve collision */
            @Override // android.os.Parcelable.Creator
            public SavedState createFromParcel(Parcel parcel) {
                return new SavedState(parcel);
            }

            /* JADX WARN: Can't rename method to resolve collision */
            @Override // android.os.Parcelable.Creator
            public SavedState[] newArray(int i) {
                return new SavedState[i];
            }
        };
        int color;
        boolean isBrightnessGradient;

        SavedState(Parcelable parcelable) {
            super(parcelable);
        }

        private SavedState(Parcel parcel) {
            super(parcel);
            this.color = parcel.readInt();
            this.isBrightnessGradient = parcel.readInt() == 1;
        }

        @Override // android.view.View.BaseSavedState, android.view.AbsSavedState, android.os.Parcelable
        public void writeToParcel(Parcel parcel, int i) {
            super.writeToParcel(parcel, i);
            parcel.writeInt(this.color);
            parcel.writeInt(this.isBrightnessGradient ? 1 : 0);
        }
    }
}
